package com.cxdr.awsips.packing.pool;

import java.util.Collection;
import java.util.HashSet;
import java.util.Set;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.TimeUnit;

public class BaseQueue<E> {
	private LinkedBlockingQueue<E> queue = new LinkedBlockingQueue<E>();
	private static final int TIME_OUT = 10;

	public synchronized boolean contains(E file) {
		return queue.contains(file);
	}

	public   void add(E file) {
		queue.add(file);
	}

	public   E take() {
		E eml = null;
		try {
			eml = queue.poll(TIME_OUT, TimeUnit.MICROSECONDS);
//			eml=  queue.take();
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		return eml;
	}
	
	public synchronized boolean addAll(Collection<? extends E> c) {
		try {
			for (E data : c) {
				queue.add(data);
			}
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}
	public synchronized boolean addAll(E[] datas) {
		try {
			Set<E> set = new HashSet<E>();
			for (E data : datas) {
				set.add(data);
			}
			return addAll(set);
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}

	public synchronized boolean isEmpty() {
		return queue.isEmpty();
	}
}
